package utils;

import android.os.Environment;
import android.util.Log;



import org.apache.log4j.Logger;

import de.mindpipe.android.logging.log4j.LogConfigurator;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogUtil {
    private static LogUtil singletonLogUtil = new LogUtil();
    public static Logger logger;
    //将构造器设置为private禁止通过new进行实例化
    private LogUtil() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = dateFormat.format(new Date());
        String dir = Environment.getExternalStorageDirectory().toString() + "/Logs/";
        {
            File file=new File(dir);
            file.mkdir();
        }

        String fileName =  time + ".log";
        String path = dir + fileName;

        LogConfigurator logConfigurator = new LogConfigurator();
        logConfigurator.setFileName(path);

        // set max. number of backed up log files
        int maxBackupSize = 7;
        logConfigurator.setMaxBackupSize(maxBackupSize);

        // set max. size of log file
        long maxFileSize = 1024*1024;
        logConfigurator.setMaxFileSize(maxFileSize);

        logConfigurator.configure();

        Log.d("MainActivity", "LOGPATH : " + path);
        logger = org.apache.log4j.Logger.getLogger("MainActivity");
    }
    public static Logger getInstance() {
        return singletonLogUtil.logger;
    }
}
